db.js ➔ cliRead   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A db.js ➔ ... ➔ ??? 0 1 1
1
import os from 'os';
2
import fs from 'fs';
3
import path from 'path';
4
import { promisify, reject, resolve } from 'bluebird';
5
import rimraf from 'rimraf';
6
import {
7
    T,
8
    cond,
9
    equals,
10
    tryCatch,
11
    type
12
} from 'ramda';
13
import { render } from 'prettyjson';
14
15
const rm = promisify(rimraf);
16
const createFolder = promisify(fs.mkdir);
17
const createFile = promisify(fs.writeFile);
18
const readFile = promisify(fs.readFile);
19
20
/**
21
 * Marshalls the JS input
22
 *
23
 * @param {Mixed} input
24
 * @return {Promise}
25
 */
26
function serialize(input) {
27
    const value = JSON.stringify(input);
28
    return value === undefined
29
        ? reject(new Error(`Unsupported type ${type(input)}`))
30
        : resolve(value);
31
}
32
33
/**
34
 * Gets the location of a database file
35
 *
36
 * @param {String} name - Database name
37
 * @return {String}
38
 */
39
function location(name) {
40
    return path.join(os.homedir(), '.rung', `${name}.db`);
41
}
42
43
/**
44
 * Clears a database file by removing it
45
 *
46
 * @param {String} name - Database name
47
 * @return {Promise}
48
 */
49
export function clear(name) {
50
    return rm(location(name));
51
}
52
53
/**
54
 * Creates or updates a database file
55
 *
56
 * @param {String} name - Database name
57
 * @param {Mixed} store - Content to save
58
 * @return {Promise}
59
 */
60
export function upsert(name, store) {
61
    // When store is undefined, drop the file
62
    return store === undefined
63
        ? resolve()
64
        : resolveRungFolder()
65
            .then(~serialize(store))
66
            .then(value => createFile(location(name), value));
67
}
68
69
/**
70
 * Reads data from database file
71
 *
72
 * @param {String} name
73
 * @return {Promise}
74
 */
75
export function read(name) {
76
    const file = location(name);
77
    return readFile(file, 'utf-8')
78
        .then(JSON.parse)
79
        .catchReturn(undefined);
80
}
81
82
/**
83
 * Creates the .rung folder when it doesn't exist
84
 *
85
 * @return {Promise}
86
 */
87
function resolveRungFolder() {
88
    const folder = path.join(os.homedir(), '.rung');
89
    const createIfNotExists = tryCatch(
90
        ~(fs.lstatSync(folder).isDirectory()
91
            ? resolve()
92
            : reject(new Error('~/.rung is not a directory'))),
93
        ~createFolder(folder)
94
    );
95
96
    return createIfNotExists();
97
}
98
99
function getPackage() {
100
    return readFile('package.json')
101
        .then(JSON.parse);
102
}
103
104
function cliRead() {
105
    return getPackage()
106
        .then(({ name }) => read(name))
107
        .then(render)
108
        .tap(console.log)
109
        .catch(~reject(new Error('Unable to read database')));
110
}
111
112
function cliClear() {
113
    return getPackage()
114
        .then(({ name }) => clear(name))
115
        .catch(~reject(new Error('Unable to clear database')));
116
}
117
118
export default ({ option }) => option | cond([
119
    [equals('read'), cliRead],
120
    [equals('clear'), cliClear],
121
    [T, option => reject(new Error(`Unknown option ${option}`))]
122
]);
123